<?php
namespace app\manage\controller;
obj('api/Api')->isSession('manage_system','index.php?r=manage/login/index');
class findController extends \app\base\controller\BaseController
{

	public function index(){
		$this->pagetext=array("发现管理","文章列表");
		$where[] = "1";
    	$baseurl = "index.php?r=manage/find/index";
        $Page = obj('api/ApiData')->page("50", "article", $where, "`id` DESC", $baseurl);
        $this->Page = $Page;
		$this->display();
	}

	public function addarticle(){
		if(!IS_POST){
			$this->pagetext=array("发现管理","发布文章");

    		//加载马甲号
    		$vestlist=obj("index/global","controller")->vestlist("y");
    		$this->vestlist=$vestlist;

    		//加载分类
            $type=obj("index/global","controller")->atype("y","null");

            $this->type=$type;
    		$this->display();
			exit;
		}else{
			self::CheckarticleForm();
			 $data = obj('api/Api')->Form($this->POSTarg());
			 $data['date']=date("Y-m-d H:i:s",time());
			 $data['body']=$_POST['body'];
			 obj('api/ApiData')->Inserts('article', $data);
			//echo json_encode(array("info" => "保存成功", "status" => "y"));
			 $url="index.php?r=manage/find/index";
			 $this->redirect($url, $code = 302);


		}
	}

	public function editorarticle(){

		if(!IS_POST){
    		$this->pagetext=array("发现管理","编辑文章");
    		//加载马甲号
    		$vestlist=obj("index/global","controller")->vestlist("y");
    		$this->vestlist=$vestlist;

    		//加载分类
            $type=obj("index/global","controller")->atype("y","null");

            $this->type=$type;
            $id=$this->arg("id");
            $where[]="`id` ={$id}";
            $ret=obj("api/Apidata")->Data_Select("article",$where);
            $this->ret=$ret;
            $this->html='<input type="hidden" name="id" value="'.$ret['id'].'" />';
			$this->display('app/manage/view/find/addarticle');
			exit;
		}else{

			 $id=$this->arg("id");
			 $where[]="  `id` ={$id} ";
			 $data = obj('api/Api')->Form($this->POSTarg());

			 $data['body']=$_POST['body'];


             obj("api/Apidata")->Data_Updata("article",$data,$where);
             $url="index.php?r=manage/find/index";
			 $this->redirect($url, $code = 302);
			
		}

	}

	public function delete(){
		error_reporting('0');
		$id=$this->arg("id");

		//删除文章
        $where="  `id` ={$id}";
        obj('api/ApiData')->Deletethis('article', $where);

        //删除评论
        $where_c="`aid` ={$id}";
        obj('api/ApiData')->Deletethis('comment', $where_c);
        exit(json_encode(array("info" => "删除成功", "status" => "y")));
	} 


	public function type(){
		$this->pagetext=array("分类管理","管理发现分类");
		$where[] = "1";
    	$baseurl = "index.php?r=manage/find/type";
        $Page = obj('api/ApiData')->page("50", "nav", $where, "`id` DESC", $baseurl);
        $this->Page = $Page;
		$this->display();
	}

	public function addtype(){

		if(!IS_POST){
			$this->pagetext=array("分类管理","新建分类");
			$this->color=self::diycolor();
			$this->display();
			exit;
		}else{
			self::CheckTypeForm();
			 $data = obj('api/Api')->Form($this->POSTarg());
			 obj('api/ApiData')->Inserts('nav', $data);
			 echo json_encode(array("info" => "保存成功", "status" => "y"));

		}
	}

	public function editortype(){
     if(!IS_POST){
			$this->pagetext=array("分类管理","编辑分类");
			$id=$this->arg("id");
            $where[]="`id` ={$id}";
            $ret=obj("api/Apidata")->Data_Select("nav",$where);
            $this->ret=$ret;
            $this->html='<input type="hidden" name="id" value="'.$ret['id'].'" />';
            $this->color=self::diycolor();
			$this->display('app/manage/view/find/addtype');
			exit;
		}else{
			 self::CheckTypeForm();
             $id=$this->arg("id");
			 $where[]="  `id` ={$id} ";
			 $data = obj('api/Api')->Form($this->POSTarg());
             obj("api/Apidata")->Data_Updata("nav",$data,$where);
             echo json_encode(array("info" => "保存成功", "status" => "y"));
		}

	}

	public function deletetype(){

		$id=$this->arg("id");
		$where[] = "`navid` ={$id}";
		//查询该分类下面有没有文章
		$count = obj("api/ApiData")->Data_Count("article", $where);

		if($count>0){
			exit(json_encode(array("info" => "请先删除改分类下的文章", "status" => "n")));
		}

		//删除
		$where="  `id` ={$id}";
        obj('api/ApiData')->Deletethis('nav', $where);
        exit(json_encode(array("info" => "删除成功", "status" => "y")));



	}

	public function navcount($id){
       
            $where[] = "`navid` ={$id}";
            $count = obj("api/ApiData")->Data_Count("article", $where);
            echo $count;
        
    }


	public function CheckTypeForm(){

		if(!$this->arg("name")){
			exit(json_encode(array("info" => "请填写名称", "status" => "n")));
		}
		if(!$this->arg("pic")){
			exit(json_encode(array("info" => "请上传图标", "status" => "n")));
		}
		if(!$this->arg("keywords")){
			exit(json_encode(array("info" => "请输入关键字", "status" => "n")));
		}
		if(!$this->arg("dec")){
			exit(json_encode(array("info" => "请输入描述", "status" => "n")));
		}

	}

	public function CheckarticleForm(){

		if(!$this->arg("title")){
			exit(json_encode(array("info" => "请填写标题", "status" => "n")));
		}
		if($this->arg("navid")=='0'){
			exit(json_encode(array("info" => "请选择分类", "status" => "n")));
		}
		if(!$this->arg("pic")){
			exit(json_encode(array("info" => "请上传一张封面", "status" => "n")));
		}
		if(!$this->arg("keywords")){
			exit(json_encode(array("info" => "请输入关键字", "status" => "n")));
		}
		if(!$this->arg("dec")){
			exit(json_encode(array("info" => "请输入描述", "status" => "n")));
		}
		if($this->arg("uid")=='0'){
			exit(json_encode(array("info" => "请选择一个虚拟马甲", "status" => "n")));
		}
	}


	//自定义颜色
	public function diycolor(){

		$array=array("#ac725e","#d06b64","#f83a22","#fa573c","#ff7537","#ffad46","#42d692","#16a765","#7bd148","#b3dc6c","#fbe983","#fad165","#92e1c0","#9fe1e7","#9fc6e7","#4986e7","#9a9cff","#c2c2c2","#cabdbf","#cca6ac","#f691b2","#cd74e6","#a47ae2","#555","#4600e4");
		return $array;
	}
}